package cn.yj.core.geo;

import cn.yj.tools.exception.Error;
import cn.yj.tools.exception.ServiceException;
import cn.yj.tools.http.request.AbstractRequest;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * <p>
 *
 * </p>
 *
 * @author 永健
 * @since 2019-09-14 10:13
 */
@Component
class GeoRequest extends AbstractRequest {
    private final String GEO_REQ_SUCCESS_KEY = "infocode";
    private final String GEO_REQ_SUCCESS_VALUE = "10000";


    @Override
    protected <T> T filterResult(T t) throws Exception {
        Map<String, Object> map;
        if (t instanceof Map) {

            map = (Map<String, Object>) t;


        } else if (t instanceof String) {
            map = JSON.parseObject(t.toString(), Map.class);
        } else {
            return t;
        }

        String code = map.get(GEO_REQ_SUCCESS_KEY).toString();
        if (GEO_REQ_SUCCESS_VALUE.equals(code)) {
            return t;
        }

        throw new ServiceException(Error.Http接口响应异常, map.get("info").toString());
    }
}
